﻿using ShopPowerTemplate.Common;
using ShopPowerTemplate.Entities;
using ShopPowerTemplate.WebServiceReference;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.ApplicationModel.DataTransfer;
using Windows.Storage.Streams;
using Windows.UI.Xaml.Media.Imaging;

namespace ShopPowerTemplate.ViewModels
{
    public class ProductPresenterViewModel : BindableBase
    {
        Product _product = null;
        public Product Product
        {
            get { return this._product; }
            set
            {
                if (this.SetProperty(ref this._product, value))
                    InitializeImage();
            }
        }

        ObservableCollection<ImagePresenterViewModel> _imagePresenterList = new ObservableCollection<ImagePresenterViewModel>();
        public ObservableCollection<ImagePresenterViewModel> ImagePresenterList
        {
            get { return this._imagePresenterList; }
            set { this.SetProperty(ref this._imagePresenterList, value); }
        }

        public ProductPresenterViewModel(int productId)
        {
            LoadProduct(productId);
        }

        public ProductPresenterViewModel(Product product) : this(product.Id) { }

        public async void LoadProduct(int id)
        {
            WebServiceClient service = new WebServiceClient();
            Product aux = await service.FindProductAsync(id);
            aux.Reviews = (await service.ProductGetReviewsForAsync(aux)).ToList();
            Product = aux;
        }

        private async void InitializeImage()
        {
            WebServiceClient service = new WebServiceClient();
            var image = await service.GetProductFirstImageAsync(Product.Id);

            if (image != null)
            {
                ImagePresenterList.Add(new ImagePresenterViewModel(image));
            }
        }

    }
}
